package b1w1nn1ng.crossf1t.business;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import b1w1nn1ng.crossf1t.db.Database;

public class AMRAP extends Workout {
		private int timeLimit; //in minutes
		private int roundsCompleted;
				
		public AMRAP(int userID, boolean prescribed, Calendar date) throws SQLException, ClassNotFoundException {
			this.userID = userID;
			this.type = "amrap";
			exercises = new ArrayList<Exercise>();
			this.prescribed = prescribed;
			this.cal = date;
			Workout.incrementWorkoutCount(userID);
		}
		
		public AMRAP(int userID, int timeLimit, int roundsCompleted, ArrayList<Exercise> exercises, boolean prescribed, Calendar cal) throws SQLException, ClassNotFoundException{
			this.userID = userID;
			this.timeLimit = timeLimit;
			this.roundsCompleted = roundsCompleted;
			this.type = "AMRAP";
			this.exercises = exercises;
			this.prescribed = prescribed;
			this.cal = cal;
			//I DON'T ALWAYS PROGRAM BUT WHEN I DO I PROGRAM LIKE A BOSS
			Workout.incrementWorkoutCount(userID);
		}

		public String toHTML() {
			StringBuilder ret = new StringBuilder();
			ret.append("Workout type: "+type+"<br />");
			ret.append("Date: "+CalendarDisplay.dateToString(cal)+"<br />");
			ret.append("Time limit: "+timeLimit+"<br />");
			ret.append("Rounds completed: "+roundsCompleted+"<br />");
			ret.append("<br />Exercises: <br />");
				for(Exercise e : exercises) 
					ret.append(e.toHTML()+"<br />");
			ret.append(prescribed?"Performed as prescribed\n":"Not performed as prescribed<br />");
			return ret.toString();
		}
		
		@Override
		public boolean isPr(int id, String type,Exercise[] exercises, int value) throws Exception {
			Workout w = getPR(id, type,exercises);
			if(! (w instanceof AMRAP))
				throw new Exception("getPR did not return an AMRAP object");
			
			return ((AMRAP)w).roundsCompleted == value;
		}


		public int getTimeLimit() {
			return timeLimit;
		}


		public void setTimeLimit(int timeLimit) {
			this.timeLimit = timeLimit;
		}


		public int getRoundsCompleted() {
			return roundsCompleted;
		}


		public void setRoundsCompleted(int roundsCompleted) {
			this.roundsCompleted = roundsCompleted;
		}

		@Override
		public int compareTo(Workout o) {			
			return this.roundsCompleted-((AMRAP)o).getRoundsCompleted();
		}


		public boolean isPrescribed() {
			return prescribed;
		}


		public void setPrescribed(boolean prescribed) {
			this.prescribed = prescribed;
		}


		public Calendar getDate() {
			return cal;
		}


		public void setDate(Calendar cal) {
			this.cal = cal;
		}


		public ArrayList<Exercise> getExercises() {
			return exercises;
		}


		public void setExercises(ArrayList<Exercise> exercises) {
			this.exercises = exercises;
		}


		public String getType() {
			return type;
		}


		public void setType(String type) {
			this.type = type;
		}

		public void setUserID(int userID) {
			this.userID = userID;
		}
		
		@Override
		public void writeToDatabase() {
			try {
				Database.addWorkout(this);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		


	
}